package feishu

import (
	"sync"
)

// Config .
type Config struct {
	WebHook   string `json:"web_hook" yaml:"web_hook"`
	SecretKey string `json:"secret_key" yaml:"secret_key"`
}

// const .
const (
	MsgTypeText        = "text"
	MsgTypePost        = "post"
	MsgTypeImage       = "image"
	MsgTypeInteractive = "interactive"

	LanguageZhCn = "zh_cn"

	TagText = "text"

	TypeDefault                             = "default"
	Message                                 = "这是一个告警消息!"
	MessageDidPurchase                      = "DID购买任务"
	MessageDidImport                        = "DID导入任务"
	MessageExceedPrefixLimit                = "超过限制数量的部分，需要新的前缀码！"
	MessageTotalEqualPrefixLimit            = "总量等于限制数量，需要新的前缀码！"
	MessageUnreviewedOrder                  = "此条预订单已存在，当前状态未审核！"
	MessageReviewedOrder                    = "此条预订单未完成（已审核）！"
	MessageCompletedOrder                   = "此条预订单已完成！"
	MessagePrefixOrderRepeat                = "此条预订单重复！"
	MessageDidConfRepeat                    = "此条DID配置项重复生成预订单！"
	MessageDeletedOrder                     = "此条预订单已删除！"
	MessageSettledOrder                     = "此条预订单已结算！"
	MessageReviewedPrefixOrderNotExist      = "无已审核预订单，无需导入DID信息！"
	MessagePrefixOrderUnimport              = "此条预订单尚云未完成！"
	MessageOrderMismatchPrefixOrder         = "尚云写入的订单信息与预订单不一致！"
	MessageOriginalOrderNotExist            = "此条订单尚云未写入原始DID信息表！"
	MessageOriginalOrderMismatchPrefixOrder = "尚云写入的原始DID数量与预订单不一致！"
	WebhookURL                              = "https://open.feishu.cn/open-apis/bot/v2/hook/55767c45-52c7-4605-9d2d-3791aa2606b9"
	OpenIdEthan                             = "ou_14ecae362d39c3af6917cd48302e4e90"
)

// Response .
type Response struct {
	Code          int    `json:"code"`
	Msg           string `json:"msg"`
	StatusCode    int    `json:"StatusCode"`
	StatusMessage string `json:"StatusMessage"`
}

// PostItem .
type PostItem struct {
	Tag  string  `json:"tag"`
	Text string  `json:"text"`
	Href *string `json:"href,omitempty"`
}

// PostLanguage .
type PostLanguage struct {
	Title   string       `json:"title"`
	Content [][]PostItem `json:"content"`
}

// PostContent .
type PostContent map[string]PostLanguage

// PayloadContent .
type PayloadContent struct {
	Text     *string      `json:"text,omitempty"`
	Post     *PostContent `json:"post,omitempty"`
	ImageKey *string      `json:"image_key,omitempty"`
}

// CardConfg .
type CardConfg struct {
	WideScreenMode bool `json:"wide_screen_mode"`
	EnableForward  bool `json:"enable_forward"`
}

// CardItem .
type CardItem struct {
	Tag     string `json:"tag"`
	Content string `json:"content"`
}

// CardAction .
type CardAction struct {
	Tag   string       `json:"tag"`
	Text  CardItem     `json:"text"`
	URL   string       `json:"url"`
	Type  string       `json:"type"`
	Value *interface{} `json:"value,omitempty"`
}

// CardElement .
type CardElement struct {
	Tag     string       `json:"tag"`
	Text    CardItem     `json:"text"`
	Actions []CardAction `json:"actions"`
}

// CardHeader .
type CardHeader struct {
	Title CardItem `json:"title"`
}

// InteractiveContext .
type CardContent struct {
	Config   CardConfg     `json:"config"`
	Elements []CardElement `json:"elements"`
	Header   CardHeader    `json:"header"`
}

// PayLoad .
type PayLoad struct {
	MsgType   string          `json:"msg_type"`
	Timestamp string          `json:"timestamp"`
	Sign      string          `json:"sign"`
	Content   *PayloadContent `json:"content,omitempty"`
	Card      *CardContent    `json:"card,omitempty"`
}

var feiClient *FeiClient
var once sync.Once

// SetConfig .
func SetConfig(conf Config) {
	GetClient().SetConfig(conf)
}

// GetClient .
func GetClient() *FeiClient {
	once.Do(func() {
		feiClient = &FeiClient{}
	})
	return feiClient
}
